﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace MINERP.DAL.InfoConfig
{
    public  class UserRoleDAL:DALBase
    {
        private static ObjectQuery<tb_Power> GetPower()
        {
            return DataEntites.tb_Power.Include("tb_Tech");
        }
        private static ObjectQuery<tb_Tech> GetTech()
        {
            return DataEntites.tb_Tech;
        }
        /// <summary>
        /// 获得单个节点对象
        /// </summary>
        /// <param name="techName"></param>
        /// <returns></returns>
        public static tb_Tech GetTechObj(string techName)
        {
            return DataEntites.tb_Tech.First(t => t.TechName == techName);
        }
        /// <summary>
        /// 获得单个用户及其操作权限
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static tb_Power GetPowerAndTech(string powerName)
        {
            return DataEntites.tb_Power.Include("tb_Tech").First(p => p.Name == powerName);
        }

        public static tb_Power GetPowerObj(string name)
        {
            return DataEntites.tb_Power.First(p => p.Name == name);
        }
        /// <summary>
        /// 获得角色list
        /// </summary>
        /// <returns></returns>
        public static List<tb_Power> GetPowerList()
        {
            List<tb_Power> list = new List<tb_Power>();
            ObjectQuery<tb_Power> powers = GetPower();
            foreach (var power in powers)
            {
                list.Add(power);
            }
            return list;
        }

        /// <summary>
        /// 获得节点list
        /// </summary>
        /// <returns></returns>
        public static List<tb_Tech> GetTechList()
        {
            List<tb_Tech> list = new List<tb_Tech>();
            ObjectQuery<tb_Tech> techs = GetTech();
            foreach (var tech in techs)
            {
                list.Add(tech);
            }
            return list;
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="power"></param>
        public static void AddRole(tb_Power power)
        {
            DataEntites.AddTotb_Power(power);
            DataEntites.SaveChanges();
        }
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="techList"></param>
        public static void UpdateRole(tb_Power obj, List<tb_Tech> techList)
        {
            tb_Power power = DataEntites.tb_Power.First(p => p.PowerId == obj.PowerId);
            power.tb_Tech.Clear();
            foreach (tb_Tech tech in techList)
            {
                power.tb_Tech.Add(tech);
            }
            power.Name = obj.Name;
            power.Des = obj.Des;
            power.AssemblyName = obj.AssemblyName;
            power.FrmName = obj.FrmName;
            DataEntites.SaveChanges();
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="obj"></param>
        public static void DeleteRole(tb_Power obj)
        {
            tb_Power power = DataEntites.tb_Power.First(p =>p.PowerId ==obj.PowerId );
            DataEntites.DeleteObject(power);
            DataEntites.SaveChanges();
        }

    }
}
